import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "../views/layout/Layout";
import Login from "../views/Login/Login";
import statement from "../views/statement/index.vue";
import waringlist from "../views/waringlist/index.vue";

import energystation from "../views/energystation/index.vue";
import energyunit from "../views/energyunit/index.vue";
import pcs from "../views/psc/index.vue";
import batterystack from "../views/batterystack/index.vue";

import batterycluster from "../views/batterycluster/index.vue";
import basemap from "../views/basemap/index.vue";
import photovoltaic from "../views/photovoltaic/index.vue";

import cost from "../views/cost/index.vue";

import control from "../views/control/index.vue";
import SitePhotos from "../views/site-photos/index.vue";

import historyinfo from "../views/historyinfo/index.vue";

import store from "@/store";
import { getToken } from "@/utils/auth";
Vue.use(VueRouter);
// 获取原型对象push函数
const originalPush = VueRouter.prototype.push;

// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace;

// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch((err) => err);
};

// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace(location) {
	return originalReplace.call(this, location).catch((err) => err);
};

const routes = [
	{
		path: "/",
		redirect: "/login",
	},
	{
		path: "/login",
		name: "login",
		component: Login,
	},
	{
		path: "/lunbo",
		name: "lunbo",
		component: () => import("../views/lunbo/index.vue"),
		meta: { title: "轮播页面", icon: "lunbo" },
	},
	{
		path: "/layout",
		component: Layout,
		name: "layout",
		// redirect: "/layout",
		meta: { title: "首页", icon: "home" },
		children: [
			{
				path: "",
				redirect: "/statement",
			},
			{
				path: "/statement",
				name: "statement",
				component: statement,
				meta: { title: "报表", icon: "statement" },
			},
			{
				path: "/waringlist",
				name: "waringlist",
				component: waringlist,
				meta: { title: "报警信息", icon: "waringlist" },
			},
			{
				path: "/energystation",
				name: "energystation",
				component: energystation,
				meta: { title: "储能站", icon: "energystation" },
			},
			{
				path: "/energyunit",
				name: "energyunit",
				component: energyunit,
				meta: { title: "储能单元", icon: "energyunit" },
			},
			{
				path: "/pcs",
				name: "pcs",
				component: pcs,
				meta: { title: "PCS", icon: "pcs" },
			},
			{
				path: "/batterystack",
				name: "batterystack",
				component: batterystack,
				meta: { title: "电池堆", icon: "batterystack" },
			},
			{
				path: "/batterycluster",
				name: "batterycluster",
				component: batterycluster,
				meta: { title: "电池簇", icon: "batterycluster" },
			},
			{
				path: "/basemap",
				name: "basemap",
				component: basemap,
				meta: { title: "地图索引", icon: "basemap" },
			},
			// {
			// 	path: "/photovoltaic",
			// 	name: "photovoltaic",
			// 	component: photovoltaic,
			// 	meta: { title: "光伏", icon: "photovoltaic" },
			// },
			{
				path: "/cost",
				name: "cost",
				component: cost,
				meta: { title: "计费管理", icon: "cost" },
			},
			{
				path: "/control",
				name: "control",
				component: control,
				meta: { title: "控制策略", icon: "control" },
			},
			{
				path: "/site-photos",
				name: "site-photos",
				component: SitePhotos,
				meta: { title: "现场风采", icon: "site-photos" },
			},
			{
				path: "/historyinfo",
				name: "historyinfo",
				component: historyinfo,
				meta: { title: "历史信息", icon: "historyinfo" },
			},
		],
	},
	// 大屏
	// {
	// 	path: "/home",
	// 	name: "home",
	// 	component: () =>
	// 		import( "../views/Home/home.vue"),
	// },
];
const router = new VueRouter({
	mode: "hash",
	// base: process.env.BASE_URL,
	routes,
});

// 当路由准备就绪时，将路由加载到Vuex store
router.onReady(() => {
	store.dispatch("loadRoutes", routes);
});

router.beforeEach((to, from, next) => {
	if (getToken()) {
		if (store.state.userInfo.user.nickname) {
			next();
		} else {
			store
				.dispatch("GetInfo")
				.then((res) => {
					if (res.code == 200) {
						next(); // hack方法 确保addRoutes已完成
					} else {
						next({ path: "/login" });
					}
				})
				.catch((err) => {
					store.dispatch("LogOut").then(() => {
						next({ path: "/login" });
					});
				});
		}
	} else {
		// 没有token
		if (to.path === "/login") {
			next();
		} else {
			next("/login");
		}
	}
});

export default router;
